package com.example.app2024;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class Utils
{
      private static Utils utils;
      private int mDisplayWidth;
      private int mDisplayHeight;

      private  static  final  float STANDARD_WIDTH = 720;
      private  static  final  float STANDARD_HEIGHT = 1080;

      private Utils(Context context)
      {
            if (mDisplayWidth == 0 || mDisplayHeight == 0)
            {
                  WindowManager manager = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
                  if (manager != null)
                  {
                        DisplayMetrics displayMetrics = new DisplayMetrics();
                        manager.getDefaultDisplay().getMetrics(displayMetrics);
                        if (displayMetrics.widthPixels > displayMetrics.heightPixels)  //横屏
                        {
                              mDisplayWidth = displayMetrics.heightPixels;
                              mDisplayHeight = displayMetrics.widthPixels;
                        }
                        else
                        {
                              mDisplayHeight = displayMetrics.heightPixels;
                              mDisplayWidth = displayMetrics.widthPixels - getStatusBarHeight(context);
                        }
                  }
            }

      }

      public int getStatusBarHeight(Context context)
      {
            int resID = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resID > 0)
            {
                  return context.getResources().getDimensionPixelSize(resID);
            }
            return 0;
      }

      public static Utils getInstance(Context context)
      {
            if (utils == null)
            {
                  utils = new Utils(context.getApplicationContext());

            }
            return utils;
      }

      //获取水平方向上的缩放比例
      public  float getHorizontalScale(){
            return  mDisplayWidth / STANDARD_WIDTH;
      }

      //获取垂直方向上的缩放比例
      public  float getVerticalScale(){
            return  mDisplayHeight / STANDARD_HEIGHT;
      }
}
